<template>
	<el-form :model="formData" :rules="formRules" ref="formRef" label-width="90px" label-position="left">
		<el-form-item label="金额下限：" prop="money1" @keyup.enter.native="enterKey($event, 'DPT1')">
			<el-input type="text" v-model.trim="formData.money1" autocomplete="off" maxlength="16"></el-input>
		</el-form-item>
		<el-form-item label="金额上限：" prop="money2" ref="DPT1" @keyup.enter.native="save('formRef')">
			<el-input type="text" v-model.trim="formData.money2" autocomplete="off" maxlength="16"></el-input>
		</el-form-item>
		<el-form-item>
            <el-button type="primary" size="small" @click="save('formRef')">保存</el-button>
            <el-button type="danger" size="small" @click="close">关闭</el-button>
        </el-form-item>
	</el-form>
</template>
<script>
export default {
	data() {
		var checkNumber = (rule, value, callback) => {
			if(isNaN(value)) {
				return callback(new Error('金额只能是一个数字'));
			}
			callback();
		}
		return {
			formData: {},
			formRules: {
                money1: [{required: true, message: '下限金额不能为空', triggle: 'blur'}, {validator: checkNumber, trigger: 'blur'}],
                money2: [{required: true, message: '上限金额不能为空', triggle: 'blur'}, {validator: checkNumber, trigger: 'blur'}]
            }
		}
	},
	methods: {
		save(formName) {		//保存
			this.$refs[formName].validate((valid) => {
				if (valid) {
					let store = this.$store.state.verifyProcessStore,
						money1 = Number(this.formData['money1']),
						money2 = Number(this.formData['money2']);
					if(money1 <= 0 || money2 <= 0) {
						alert('输入金额不符合条件');
						return;
					}
					if(money1 > money2) {
						alert('金额下限不能大于金额上限');
						return;
					}
					//金额不能为 0 ~ 50000000
					if((money1 === store.minMoney) && (money2 === store.maxMoney)) {
						alert('该金额已存在');
						return;
					}
					let data = {
						id: store.dfId,
						name: money1 + ' ~ ' + money2,
						level: 4,
						money1: money1,
						money2: money2,
						billId: store.billId,
						vercharId: store.vercharId,
						deptId: store.deptId,
						pid: store.empId,
						children: [],
                        subList: []
					}
					this.$store.commit('verifyProcessStore/appendNode', data);
					this.close();
				}
			});
		},
		close() {           //关闭新增弹窗
            let index = parent.layer.getFrameIndex(window.name);    //先得到当前iframe层的索引
            parent.layer.close(index);
        },
        enterKey(lastRef, nextRef) {		//回车键换行
            if(lastRef.target.value) {
                this.$refs[nextRef].focus();
            }
        }
	}
}
</script>